<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Admin;
use App\Models\Category;
use Illuminate\Http\Request;
use Request as Requests;
use Doctrine\Tests\Common\Cache\RedisCacheTest;

/**
 * Class RecycleController
 * @package App\Http\Controllers\Admin
 */
class RecycleController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdmins(Request $request)
    {
        $data = Admin::onlyTrashed()->where('name','like','%'.$request->input('keywords').'%')->paginate(3);
        $search_url = substr(url(Requests::url()),22);
        $total = Admin::onlyTrashed()->get();
        return view('admin/admin_recycle',['data' => $data,'total' => $total,'search_url' => $search_url,'request' => $request->all()]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getUsers(Request $request)
    {
        $data = User::onlyTrashed()->where('name','like','%'.$request->input('keywords').'%')->paginate(3);
        $search_url = substr(url(Requests::url()),22);
        $total = User::onlyTrashed()->get();
        return view('admin/user_recycle',['data' => $data,'total' => $total,'search_url' => $search_url,'request' => $request->all()]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getCategorys(Request $request)
    {
        $data = Category::onlyTrashed()->where('title','like','%'.$request->input('keywords').'%')->paginate(3);
        $search_url = substr(url(Requests::url()),22);
        $total = Category::onlyTrashed()->get();
        return view('admin/category_recycle',['data' => $data,'total' => $total,'search_url' => $search_url,'request' => $request->all()]);
    }

    /**
     * 数据恢复操作
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getRecover($id)
    {
        if (substr($this->getRedirectUrl(), 35) == 'admins') {
            $result = Admin::onlyTrashed()->where('id', $id)->restore();
            if ($result)
                return back()->with('msg', '恢复成功');
            else
                return back()->with('error', '恢复失败');
        }
        if (substr($this->getRedirectUrl(), 35) == 'users') {
            $result = User::onlyTrashed()->where('id', $id)->restore();
            if ($result)
                return back()->with('msg', '恢复成功');
            else
                return back()->with('error', '恢复失败');
        }
        if (substr($this->getRedirectUrl(), 35) == 'categorys') {
            $result = Category::onlyTrashed()->where('id', $id)->restore();
            dd($result);
            if ($result)
                return back()->with('msg', '恢复成功');
            else
                return back()->with('error', '恢复失败');
        }
    }
}

